<template>
  <div style="width: 100%; height: 100%;">
    <div id="map" class="map" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import LineString from 'ol/geom/LineString';
import GeoCircle from 'ol/geom/Circle';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import LayerVector from 'ol/layer/Vector';
import SourceVector from 'ol/source/Vector';

import Text from 'ol/style/Text';
import Icon from 'ol/style/Icon';
import Fill from 'ol/style/Fill';
import Style from 'ol/style/Style';
import Circle from 'ol/style/Circle';
import Stroke from 'ol/style/Stroke';


export default {
    components: {},
    data() {
        return {
            map: null
        };
    },
    mounted() {
        this.initMap();
    },
    methods: {
        initMap() {
            // 一个空白图层，可以理解成画布，我们要在上面绘制各种图形
            let layerVector = new LayerVector({
                source: new SourceVector()
            });

            let map = new Map({
                view: new View({
                    center: [12950000, 4860000],
                    zoom: 7
                }),
                layers: [
                    // 底图
                    new TileLayer({
                        source: new XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}'})
                    }),
                    layerVector
                ],
                target: 'map',
            });

            // 一个点击事件
            map.on('click', function (evt) {
                // 鼠标点击的坐标
                let coordinate = evt.coordinate;
                console.log('click: ' + coordinate);

                map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
                    Layers.succeed('功能建设中！');
                });
            });

            this.map = map;

            //样式
            let style = new Style({
                stroke: new Stroke({
                    color: 'blue',
                    lineDash: [4],
                    width: 3
                }),
                fill: new Fill({
                    color: 'rgba(0, 0, 255, 0.5)'
                })
            });

            // 特征点
            let feature = new Feature({
                geometry: new GeoCircle([12950000, 4860000], 10000)
            });

            feature.setStyle(style);
            layerVector.getSource().addFeature(feature);
        }
    }
};
</script>
